using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{
	/// <summary>
	/// Author: Juin Min
	/// </summary>
	public class SVNRemove : CommandBase
	{
		private java.io.File filepath;
		private SVNURL[] urlpath = new SVNURL[1];
        private bool deleteFiles = true;
		private bool WConly;
        private bool dryRun;
		
		public SVNRemove(string working_path)
		{
			Debug.WriteLine("SVN Remove std constructor");
			WConly = this.checkPath(working_path);
			if (WConly)
            {
				filepath = new java.io.File(working_path);
				urlpath[0] = null;
			}
			else
            {
				urlpath[0] = SVNURL.parseURIEncoded(working_path);
			}
		}

        public SVNRemove(string working_path, bool deleteFiles, bool dryRun)
        {
            Debug.WriteLine("SVN Remove std constructor");
            WConly = this.checkPath(working_path);
            if (WConly)
            {
                filepath = new java.io.File(working_path);
                urlpath[0] = null;
            }
            else
            {
                filepath = null;
                urlpath[0] = SVNURL.parseURIEncoded(working_path);
            }
            this.deleteFiles = deleteFiles;
            this.dryRun = dryRun;
        }
		
		public override void Execute()
		{
			Debug.WriteLine("SVN Remove Execution");
					
			// Setup library
			Tools.SetupLibrary();
			
			// Setup clients
			SVNClientManager cm = SVNClientManager.newInstance();
						
			try
            {
				if (WConly)
                {
					SVNWCClient wcclient = cm.getWCClient();
					wcclient.doDelete(filepath,true,this.deleteFiles,this.dryRun);
				}
				else
                {
					SVNCommitClient commitclient = cm.getCommitClient();
					commitclient.doDelete(urlpath,"Deletion");
				}	
			}
            catch (SVNException exptn)
            {
				throw new SVNExecuteException(exptn.getMessage());
			}
		}
		
		private bool checkPath(string inputPath)
		{
			// Checks if path is for repos access or WC access
			// Returns true for WC access (used for bool member WCOnly)
			Debug.WriteLine("SVN Remove method to check if input path is repos access");
			List<string> repos_access = new List<string>();
			// List of Repos access URLs
			repos_access.Add("file:///"); // Local repos
			repos_access.Add("http://");
			repos_access.Add("https://");
			repos_access.Add("svn://");
			repos_access.Add("svn+ssh://");
			
			foreach (string accessprefix in repos_access)
			{
				if (inputPath.StartsWith(accessprefix))
					return false;
			}
			return true;
		}
	}
}
